var emailFlag = true;
var passwrodFlag = true;
var confirmPaswordFlag = true;
var birthdayFlag = true;
var selProductId = "";

function radioListener(type) {
	if ('hyq' == type) {
		document.getElementById("czqtypediv").style.display = "none"; // 隐藏
		document.getElementById("hyqtypediv").style.display = ""; // 显示
	} else {
		document.getElementById("czqtypediv").style.display = ""; // 显示
		document.getElementById("hyqtypediv").style.display = "none"; // 隐藏
	}
}

function getfocus(type){
	document.getElementById(type).style.display = ""; // 显示
	
	if("emailInfo" == type){
		document.getElementById("emailNullError").style.display = "none";
		document.getElementById("emailError").style.display = "none";
		document.getElementById("emailExistError").style.display = "none";
	}
	
	if("passwordInfo" == type){
		document.getElementById("passwordError").style.display = "none";
		document.getElementById("passwordLenError").style.display = "none";
	}
	
	if("confirmPasswordInfo" == type){
		document.getElementById("confirmPasswordError").style.display = "none";
		document.getElementById("confirmPasswordNullError").style.display = "none";
		document.getElementById("confirmPasswordLenError").style.display = "none"; 
	}
	
	if("birthdayInfo" == type){
		document.getElementById("birthdayError").style.display = "none";
	}
	
}

function loseblur(type){
	document.getElementById(type).style.display = "none"; // 隐藏
	
	if("emailInfo" == type){
		validateEmail();
	}
	
	if("passwordInfo" == type){
		validatePassword();
	}
	
	if("confirmPasswordInfo" == type){
		validateConfirmPassword();
	}
	
	if("birthdayInfo" == type){
		validateBirthday();
	}
}

function registerUser(){
	var registerForm = document.getElementById("registerForm");
	//var hyqRadio = document.getElementById("hyqRadio");
	var agreeBox = document.getElementById("agreeBox");
	
	validateEmail();
	validatePassword();
	validateConfirmPassword();
	validateBirthday();
	
	if(emailFlag == true && passwrodFlag == true && confirmPaswordFlag == true){
	  if(birthdayFlag == true){
		  if(!agreeBox.checked){
				alert("你还没有选择同意服务条款,你是否同意该服务条款？");
				agreeBox.checked = true;
				return;
		}
		  registerForm.submit();
	  }
	}
}

function validateBirthday(){
   var birthday = document.getElementById("czqbirthdayId").value;
   
   if(birthday != ""){
	   var age = getAge(birthday);
	   if(age < 0 || age > 6){
		   document.getElementById("birthdayError").style.display = ""; // 显示
		   birthdayFlag = false;
		   return;
	   }else{
		   document.getElementById("birthdayError").style.display = "none";
		   birthdayFlag = true; 
	   }
   }
	
	if(birthday == ""){
		document.getElementById("birthdayError").style.display = ""; // 显示
		birthdayFlag = false;
		return;	
	}else{
		document.getElementById("birthdayError").style.display = "none";
		birthdayFlag = true;
	}
}

function getAge(brith){
	var aDate=new Date();  
	var thisYear=aDate.getFullYear();
	brith=brith.substr(0,4);
	age=(thisYear-brith);
	return age;
}

function validateEmail(){
	var email = document.getElementById("emailId").value;
	
	if(email == ""){
		document.getElementById("emailNullError").style.display = ""; // 显示
		emailFlag = false;
		return;	
	}else{
		document.getElementById("emailNullError").style.display = "none";
		emailFlag = true;
	}
	
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)){
	   document.getElementById("emailError").style.display = ""; // 显示
	   emailFlag = false;
	   return;
	 }else{
		 document.getElementById("emailError").style.display = "none";
		 emailFlag = true;
	 }
	
	 validateEamilExist(email);
}

function validatePassword(){
	var password = document.getElementById("passwordId").value;
	
	if(password == ""){
		document.getElementById("passwordError").style.display = ""; // 显示
		passwrodFlag = false;
		return;	
	}else{
		document.getElementById("passwordError").style.display = "none"; 
		passwrodFlag = true;
	}
	
	if(password.length < 6 || password.length > 50){
	  document.getElementById("passwordLenError").style.display = ""; // 显示
	  passwrodFlag = false;
	  return;
	}else{
		document.getElementById("passwordLenError").style.display = "none";
		passwrodFlag = true;
	}
}

function validateConfirmPassword(){
	var confirmPassword = document.getElementById("confirmPasswordId").value;
	var password = document.getElementById("passwordId").value;
	
	if(confirmPassword == ""){
		document.getElementById("confirmPasswordNullError").style.display = ""; // 显示
		confirmPaswordFlag = false;
		return;	
	}else{
		document.getElementById("confirmPasswordNullError").style.display = "none"; 
		confirmPaswordFlag = true;
	}
	
	if(password == confirmPassword){
		if(confirmPassword.length > 5 && confirmPassword.length < 49){
			document.getElementById("confirmPasswordError").style.display = "none";
			document.getElementById("confirmPasswordLenError").style.display = "none"; 
			confirmPaswordFlag = true;
		}else{
			document.getElementById("confirmPasswordLenError").style.display = ""; // 显示
			confirmPaswordFlag = false;
		}
	}else{
		document.getElementById("confirmPasswordError").style.display = ""; // 显示
		confirmPaswordFlag = false;
	}

}

function validateEamilExist(email){
	$.ajax({url: "register/validateEmail.json", data:{email:email},async: false, success: function(data) {
		if(data == 1){
			 document.getElementById("emailExistError").style.display = ""; // 显示 
			 emailFlag =  false;
		 }else{
			 document.getElementById("emailExistError").style.display = "none";
			 emailFlag = true;
		 }  
    }});
}

function showBottomDivCss(){
	var registerBottomDiv =  document.getElementById("rigisterBottomDiv");
	var registerTopDiv =  document.getElementById("registerTopDiv");
	
	registerBottomDiv.setAttribute("class","regSelectAgesSelected");
	registerTopDiv.setAttribute("class","regSelectAgesNormal");
}

function showTopDivCss(){
	var registerBottomDiv =  document.getElementById("rigisterBottomDiv");
	var registerTopDiv =  document.getElementById("registerTopDiv");
	
	registerBottomDiv.setAttribute("class","regSelectAgesNormal");
	registerTopDiv.setAttribute("class","regSelectAgesSelected");
}


function selectProduct(param,name,price){
	if(selProductId != ""){
		$("#" + selProductId).attr("class","item");
	}
	
	selProductId = param;
	
	$("#" + param).attr("class","itemSelected");
	
	$("#productNullError").hide();
	
	$("#confirmProductName").html(name);
	$("#confirmProductPrice").html("￥"+price+"元");
	
	$("#totalAmountDiv").show();
	
	$("#totalAmount").html("合计：￥"+price+"元");
}

function submitProduct(url){
		if(selProductId != ""){
			document.getElementById("lockBodyDivId").style.display = ""; // 显示
			showCenterDiv("#registerDivId");
			$("#registerDivId").show();
			window.open(url+'?pid='+selProductId);
			document.getElementById("productNullError").style.display = "none";  
			return;
		}

		$("#productNullError").show();
}

function showCenterDiv(obj)
{
    var h1 = document.body.clientHeight;
    var h2 = document.documentElement.clientHeight;
    var isXhtml = (h2<=h1&&h2!=0)?true:false;
    var body = isXhtml?document.documentElement:document.body;
   
    var wh={left:0,top:0};
    var vwh={w:0,h:0};
    vwh.w=$.browser.msie?parseInt(body.clientWidth):parseInt(window.innerWidth);
    vwh.h=$.browser.msie?parseInt(body.clientHeight):parseInt(window.innerHeight);
    wh.left=(vwh.w/2)-parseInt($(obj).css("width"))/2;
    wh.top=body.scrollTop+(vwh.h/2)-parseInt($(obj).css("height"))/2;
    $(obj).css("top",wh.top);
    $(obj).css("left",wh.left);
    //return wh;
}

function closeRegisterWin() {
	$("#lockBodyDivId").hide();
	$("#registerDivId").hide();
}

